package zhr.chapter12;

public class MyRunnable5 implements Runnable{
    int i=0;
    @Override
    public void run() {
        System.out.println("启动线程"+Thread.currentThread().getName());
        while (true){
            addOne();
            if(i>=4)break;
            try{
                Thread.sleep(20);
            }catch (InterruptedException e){
                System.out.println(e.getMessage());
            }
        }
        System.out.println(Thread.currentThread().getName()+"线程结束");
    }
    private synchronized void addOne(){
        System.out.println(" 运 行 线 程 "+Thread.currentThread().getName()+",输出i的值:"+i);
        this.i++;
    }
}
